<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use DB;

use Markdown;

class ArticleController extends Controller
{
	//显示主页
    public function index()
    {
    	$res = DB::table('arts')->orderBy('id','desc')->paginate(2);

    	return view('art.index',['res'=>$res]);
    }
    //加载添加模版
    public function create()
    {
    	return view('art.add');
    }
    //执行添加操作
    public function store(Request $request)
    {	
    	$data = [
    		'content'=>strip_tags($_POST['content']),
    		'title'=>$_POST['title']
    	];

    	$res = DB::table('arts')->insert($data);
    	if($res){
    		return redirect('/art');
    	}else{
    		return back()->with('添加失败');
    	}
    }

    //显示内容
    public function show($id)
    {
    	$art = DB::table('arts')->where("id",'=',$id)->first();
    	$art->content=Markdown::convertToHtml($art->content);
    	return view('art.show',['art'=>$art]);
    }

    //加载修改页面
    public function edit($id)
    {
    	$art = DB::table('arts')->where("id",'=',$id)->first();
    	return view('art.edit',['art'=>$art]);
    }

    //执行内容修改
    public function update(Request $request,$id)
    {
    	
    	$res = DB::table('arts')->where('id','=',$id)->update(['title'=>$request->title,'content'=>$request->content]);
    	if($res){
    		return redirect('/art');
    	}else{
    		return back();
    	}

    }
    //执行删除的方法
    public function destroy($id)
    {
    	$bool = DB::table('arts')->where('id','=',$id)->delete();
    	return redirect('/art');

    }

}
